const Mock = require('mockjs')
const Random = Mock.Random;
const {urlParams2Obj} = require('./utils')

//生成模拟数据
let newsList = [];
for (let i = 1; i <= 36; i++) {
	newsList.push({
		id: i, //编号
		title: "新闻" + i + Random.ctitle(10, 30), //标题10-30字符
		img: Random.dataImage('100x100', 'Hello ' + i), //列表中的小图片
		content: "新闻详细内容" + i + Random.cparagraph(4, 8), //详细内容4-8段落
		count: Random.integer(2, 20), //阅读数
		dt_create: Random.datetime('yyyy-MM-dd HH:mm:ss'), //创建日期时间
	});
}

// 生成模拟API接口
Mock.mock(new RegExp('/news/list'), 'get', options => {
	let result = null;
	// let { body, type, url } = options;
	// body = JSON.parse(body);
	// let query= urlParams2Obj(url);
	console.log(options);

	result = {
		code: 1,
		msg: "成功",
		data: newsList,
	}	
	return result;
});

Mock.mock(new RegExp('/news/detail'), 'get', options => {
	let result = null;
	let { body, url } = options;
	body = JSON.parse(body);
	let query= urlParams2Obj(url);
	console.log(options,body,query);
	let id = query.id;

	result = {
		code: 1,
		msg: "成功",
		data: newsList[id],//数组下标==新闻id
	}
	return result;
});